package com.sxx.beautifulcity.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.listenerInterface.OnHomeItemClickListener;

import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/29.
 */
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.HomeViewHolder> {
    private List<Map<String, Object>> list;
    private OnHomeItemClickListener listener;

    public void setOnHomeItemClickListener(OnHomeItemClickListener listener){
        this.listener = listener;
    }
    public void addData(List<Map<String, Object>> list){
        this.list = list;
        notifyDataSetChanged();

    }

    class HomeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView home_text;
        private LinearLayout home_lin;
        private int position;
        public HomeViewHolder(View itemView) {
            super(itemView);
            home_text = (TextView) itemView.findViewById(R.id.home_text);
            home_lin = (LinearLayout) itemView.findViewById(R.id.home_lin);
            home_lin.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            if (list != null && list.size() > 0){
                String str = (String) list.get(position).get("homes");
                if (listener != null){
                    listener.OnHomeItemClick(str);
                }
            }
        }
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recyclerview_home_fragment, null);
        return new HomeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if (list != null && list.size() > 0){
            holder.position = position;
            String name = (String) list.get(position).get("homes");
            int color = (int) list.get(position).get("colors");
            holder.home_text.setText(name);
            holder.home_lin.setBackgroundColor(color);
        }

    }

    @Override
    public int getItemCount() {
        return list != null && list.size() > 0 ? list.size() : 0;
    }
}
